<?php

namespace app\controllers;

use Yii;
use app\tools\Re;
use app\models\Good;
use app\models\OrderInfo;
use app\models\GoodCate;
use app\models\Customer;
use app\models\CustomerCollect;
use app\models\CustomerHistory;
use app\models\CustomerAppointment;

class GoodController extends BaseController {
  public function actionGoodListOfCate(){
    $sencond_list = GoodCate::find()
      ->where(['status' => 1])
      ->andWhere(['>', 'parent_id', 0])
      ->orderBy('sort desc, created_at desc')
      ->all();
    $data = [];
    if ($sencond_list) {
      foreach($sencond_list as $v) {
        $good_data = $this->getGoodByCate($v, '', 3, 1);
        if ($good_data) {
          $data[] = [
            'id' => $v->id,
            'parent_id' => $v->parent_id,
            'name' => $v->name,
            'good_list' => $good_data
          ];
        }
      }
    }
    return Re::data(['data' => $data]);
  }

  public function actionGoodListByCate(){
    $cate_id = Yii::$app->request->post('cate_id', 0);
    $keyword = Yii::$app->request->post('keyword', '');
    $good_cate = GoodCate::findOne($cate_id);
    if (empty($good_cate)) {
      return Re::data(null, '分类信息读取失败', Re::ERROR);
    }
    $data = $this->getGoodByCate($good_cate, $keyword, 0, 2);
    return Re::data(['data' => $data]);
  }

  public function actionGoodDetail(){
    $good_id = Yii::$app->request->post('good_id', 0);
    $openid = Yii::$app->request->post('openid', '');
    $good_info = Good::findOne($good_id);
    if (empty($good_info)) {
      return Re::data(null, '商品信息读取失败', Re::ERROR);
    }
    if ($openid) {
      CustomerHistory::saveRecord($good_id, $openid);
    }
    $data = [
      'id' => $good_id,
      'cate_id' => $good_info->cate_id,
      'cate_name' => $good_info->goodCate->name,
      'name' => $good_info->name,
      'cover' => $good_info->cover,
      'imgs' => $good_info->imgs,
      'desc' => $good_info->desc,
      'buy_type' => $good_info->buy_type,
      'price' => $good_info->price,
      'unit_name' => $good_info->unit_name,
      'sale_count' => $good_info->sale_base + OrderInfo::getSaleCount($good_id) + CustomerAppointment::getAppointmentCount($good_id),
      'is_collect' => CustomerCollect::isCollect($openid, $good_id),
    ];
    return Re::data(['data' => $data]);
  }

  public function actionCollectGood(){
    $good_id = Yii::$app->request->post('good_id', '');
    $openid = Yii::$app->request->post('openid', '');
    $customer_info = Customer::find()->where(['openid' => $openid])->one();
    if (!$customer_info) {
      return Re::data(null, '用户信息读取错误', Re::ERROR);
    }
    $good_info = Good::findOne($good_id);
    if (!$good_info) {
      return Re::data(null, '商品信息读取错误', Re::ERROR);
    }
    $collect_info = CustomerCollect::find()->where(['customer_id' => $customer_info->id, 'good_id' => $good_id])->one();
    if (!$collect_info) {
      $collect_info = new CustomerCollect();
      $collect_info->is_collect = 1;
    } else {
      $collect_info->is_collect = $collect_info->is_collect ? 0 : 1;
    }
    $collect_info->customer_id = $customer_info->id;
    $collect_info->good_id = $good_id;
    $collect_info->created_at = time(); 
    $collect_info->save();
    return Re::data();
  }

  private function getGoodByCate($obj, $keyword, $limit=0, $type=1){
    $query = Good::find()->where(['status' => 1])->orderBy('sort desc, created_at desc');
    if ($type == 1) {
      $query->andWhere(['cate_id' => $obj->id]);
    } else {
      if ($obj->parent_id) {
        $query->andWhere(['cate_id' => $obj->id]);
      } else {
        $cate_arr = GoodCate::find()->where(['parent_id' => $obj->id])->select('id')->column();
        if ($cate_arr) {
          $query->andWhere(['cate_id' => $cate_arr]);
        } else {
          $query->andWhere(['cate_id' => 0]);
        }
      }
    }
    if ($keyword) {
      $query->andWhere(['like', 'name', $keyword]);
    }
    if ($limit) {
      $query->limit($limit);
    }
    $list = $query->all();
    $data = [];
    if ($list) {
      foreach($list as $v) {
        $data[] = [
          'id' => $v->id,
          'name' => $v->name,
          'cover' => $v->cover,
          'price' => $v->price,
          'unit_name' => $v->unit_name
        ];
      }
    }
    return $data;
  }
}
